package com.gitee.wsl.doc.prosemirror.state.command

import com.gitee.wsl.doc.prosemirror.state.Command
import com.gitee.wsl.doc.prosemirror.state.CommandDispatch
import com.gitee.wsl.doc.prosemirror.state.EditorState
import com.gitee.wsl.ext.base.ifValue
import com.gitee.wsl.func.plus


object SplitBlockKeepMarks: Command {
    override fun exec(
        state: EditorState,
        dispatch: CommandDispatch?,
    ): Boolean {
        val newDisp = CommandDispatch{ tr ->
            val marks = state.storedMarks?:(state.selection._to.parentOffset>=0) .ifValue( state.selection._from.marks(),null)
            if (!marks.isNullOrEmpty())
                tr.ensureMarks(marks)
            dispatch?.invoke(tr)
        }
        return Command.splitBlock.invoke(state, dispatch + newDisp )
    }
}

/// Acts like [`splitBlock`](#commands.splitBlock), but without
/// resetting the set of active marks at the cursor.
val Command.Companion.splitBlockKeepMarks get() = SplitBlockKeepMarks
